package com.carlinfo.back.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.carlinfo.common.util.ConstantFinalUtil;
import com.carlinfo.userscenter.users.pojo.AAdmins;
import com.carlinfo.userscenter.users.pojo.AAdminsEnum;
import com.carlinfo.userscenter.users.service.IUsersService;
import com.carlinfo.userscenter.users.service.impl.UsersServiceImpl;

/**
 * 不需要登陆就可以访问的页面
 * @author WangshMac
 */
public class NoLoginBackServlet extends HttpServlet
{
	private Logger logger = LogManager.getLogger() ; 
	private IUsersService usersService = new UsersServiceImpl();
	
	@Override
	protected void service(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException
	{
		logger.info("==NoLoginBackServlet=service==");
		/* 取信的内容;取单个
		 * http://127.0.0.1:8080/userscenter/NoLoginBackServlet?method=login&a=1&b=2&c=3
		 * 参数是键,返回值就是值
		 *  */
		String method = request.getParameter("method");
		logger.info("==method:{}==",method);
		
		if("login".equalsIgnoreCase(method))
		{
			/* 打开登陆页面 */
			this.login(request,response);
		}else if("loginSubmit".equalsIgnoreCase(method))
		{
			/* 登陆提交操作 */
			this.loginSubmit(request,response);
		}
	}

	/**
	 * 打开登陆页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		logger.info("==login==");
		/* 打开登陆页 */
		String path = "/WEB-INF/jsp/back/login.jsp";
		/* 获取Session */
		HttpSession session = request.getSession() ;
		/* 取票:admins */
		AAdmins admins = (AAdmins) session.getAttribute("adminsSess");
		if(admins != null)
		{
			/* 账号是启用的 */
			/* 登陆成功了 */
			/* 打开登陆页 */
			path = request.getContextPath() + "/back/AdminsBackServlet?method=main";
			/* 登陆成功;
			 * 客户端跳转
			 *  */
			response.sendRedirect(path);
			/* 一旦跳转了,后面的代码不执行 */
			return ; 
		}
		
		/* 服务器端跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 打开登陆页面
	 * 
	 * ~接收用户名和密码;
	 * ~按照用户名(邮箱)查询数据;
	 * 		查询到说明邮箱正确;
	 * 		查询不到,邮箱不正确,联系管理员;流程中止
	 * ~邮箱正确,验证密码是否正确;
	 * ~账户的状态是不是禁用;
	 * ~如果全部OK,就说明可以正常登陆
	 * 		登陆成功,要往session放一个令牌(门票)
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void loginSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 获取session */
		HttpSession session = request.getSession() ; 
		/* 未知错误 */
		String info = ConstantFinalUtil.INFO_JSON.getString("2") ; 
		/* 登陆登陆失败 */
		String path = "/WEB-INF/jsp/back/login.jsp";
		
		logger.info("==loginSubmit==");
		/* 接收用户名和密码; */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		
		/* 按照邮箱查询管理员;邮箱不能重复 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("email", email);
		AAdmins admins = this.usersService.findOneAdminsService(condMap);
		if(admins != null)
		{
			/* 邮箱存在,*/
			/* 验证密码 */
			if(admins.getPassword().equals(password))
			{
				/* 密码相同, */
				if(admins.getStatus() == AAdminsEnum.STATUS_ENABLE.getStatus())
				{
					/* 将管理员信息放到四大作用的session中 */
					session.setAttribute("adminsSess", admins);
					
					/* 账号是启用的 */
					/* 登陆成功了 */
					/* 打开登陆页 */
					path = request.getContextPath() + "/back/AdminsBackServlet?method=main";
					/* 登陆成功;
					 * 客户端跳转
					 *  */
					response.sendRedirect(path);
					/* 一旦跳转了,后面的代码不执行 */
					return ; 
				}else
				{
					/* 账号禁用 */
					info = ConstantFinalUtil.INFO_JSON.getString("5") ;
				}
			}else
			{
				/* 密码不正确 */
				info = ConstantFinalUtil.INFO_JSON.getString("4") ;
			}
			/* 邮箱存在,要默认显示到网页上:(华哥的喊声最大) */
			request.setAttribute("email", email);
		}else
		{
			/* 邮箱不存在 */
			info = ConstantFinalUtil.INFO_JSON.getString("3") ;
		}
		
		/* 存储信息 */
		request.setAttribute("info", info);
		/* 登陆失败,就还是登陆页面 */
		request.getRequestDispatcher(path).forward(request, response);
	}
}
